<?php

/**
 * MailogModel.php 注释
 * 
 * @package 
 * @subpackage 
 * @author rendong01 rendong237@126.com
 * 
 * : tool.php 10 2012-09-30 15:35:48Z rendong237@gmail.com $
 */
class MailogModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		$v["id"] = (int)$value["id"];
		$v["mid"] = (int)$value["mid"];
		$v["type"] = $value["type"];
		$v["toemail"] = $value["toemail"];
		$v["sendargs"] = $value["sendargs"];
		$v["title"] = $value["title"];
		$v["content"] = $value["content"];
		$v["sendtime"] = $value["sendtime"];
		$v["message"] = $value["message"];
		
		$this->db->insert("mailog", $v);
	}
	
	/**
	 * 修改
	 */
	public function change($id, array $value)
	{
		$v = array();
		isset($value["mid"]) && $v["mid"] = (int)$value["mid"];
		isset($value["type"]) && $v["type"] = $value["type"];
		isset($value["toemail"]) && $v["toemail"] = $value["toemail"];
		isset($value["sendargs"]) && $v["sendargs"] = $value["sendargs"];
		isset($value["title"]) && $v["title"] = $value["title"];
		isset($value["content"]) && $v["content"] = $value["content"];
		isset($value["sendtime"]) && $v["sendtime"] = $value["sendtime"];
		isset($value["message"]) && $v["message"] = $value["message"];

		$this->db->update("mailog", $v, 'id=' . (int)$id);
	}
	
	/**
	 * 删除
	 */
	public function del($id)
	{
		$this->db->delete("mailog", 'id=' .  (int)$id);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($mid, $message, $nowp, $per_page)
	{
		$where = ' where mid=' . $mid . '';
		if ($message != null){
			if (!empty($message))
				$where .= " and message='ok'";
			else
				$where .= " and message!='ok'";
		}
		
		$totalNum = $this->db->fetchOne ('select count(*) from mailog ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from mailog' . $where .' order by id desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchbyMail($mid, $email, $nowp, $per_page)
	{
		$where = ' where mid=' . $mid . '';
		
		$where .= " and (toemail like \"%$email%\" or title like \"%$email%\")";
	
		$totalNum = $this->db->fetchOne ('select count(*) from mailog ' . $where);
	
		require_once APP . '/application/models/Util.php';
	
		$pageInfo = Util::page ($totalNum, $per_page, $nowp);
		$start    = $pageInfo ['start'];
		$list     = $this->db->fetchAll ('select * from mailog' . $where .' order by id desc limit ' . $start . ',' . $per_page);
	
		return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	/**
	 * 显示
	 */
	public function show($id, $mid)
	{
		return $this->db->fetchRow("select * from `mailog` where id=\"?\" and mid=?", array($id, $mid));
	}
}